<?php

class DojoController extends Zend_Controller_Action
{
    public function init()
    {
        $this->logger = Marjory_Service::getLog();
        parent::init();
    }

    /**
     * Executes a search query on a given (or default) catalog,
     * returning a Dojo.Data compatible JSON string.
     *
     * @todo Contemplate if this should really be called DojoController when output format can still be overridden
     */
    public function indexAction()
    {
        if (!$this->_request->isXmlHttpRequest()) {
            $this->logger->err('DojoController called through direct request (requires XmlHttpRequest), IP: '.$_SERVER['REMOTE_IP']);
            return false;
        }
        $this->logger->debug('Entering '.__METHOD__);
        $q       = '';
        $catalog = $fields = false;
        $rows    = $start  = 0;
        $output  = 'DojoData';
        extract($this->_getAllParams(), EXTR_IF_EXISTS);
    
        $this->logger->debug('Received request to: '.$_SERVER['REQUEST_URI']);
        
        $service = new Marjory_Service($catalog ? $catalog : 'default');
        if ($output == 'DojoData') {
            $this->getResponse()->setHeader('Content-Type', 'application/json');
        }
        echo $service->search($q, $output, $fields, $rows, $start);
    }

    /**
     * Outputs a list of available search catalogs as JSON array
     */
    public function cataloglistAction()
    {
        $data    = Marjory_Service::getCatalogList();
        echo json_encode($data);
    }
}